
#include"CmnServer.h"
#include "Event.h"
//using namespace std;

int event::getpeoplenum()
{
	person* iterator = people;
	int i = 0;
	while(iterator->State != empty_person)
	{
	i++;
	iterator = iterator->next;
	}
	return i;
}

person* event::findperson(char* name_)
{
	person* iterator = people;
	while( strcmp(name_, iterator->name) )
	{
		iterator = iterator->next;
	}
	if(iterator->State == empty_person)return NULL;
	return iterator;
}
event::event(char* name_ , int start_hour = 0, int start_minute = 0, int start_day = 1, int start_month = 1, int start_year = 2013 , int end_hour = 0, int end_minute = 0, int end_day = 1, int end_month = 1, int end_year = 2013)
{
	people = new person;
	people->next = 0;
	strcpy(name ,name_);
	start_time.tm_sec = 0;
	start_time.tm_hour = start_hour;
	start_time.tm_min = start_minute;
	start_time.tm_mday = start_day;
	start_time.tm_mon = --start_month;
	start_time.tm_year = start_year-1900;
	end_time.tm_sec = 0;
	end_time.tm_hour = end_hour;
	end_time.tm_min = end_minute;
	end_time.tm_mday = end_day;
	end_time.tm_mon = --end_month;
	end_time.tm_year = end_year-1900;
}
void event::Invitepeople(char** _people, static int num)
{
	person* last = people, *prelast = people;
	while(last->State != empty_person)
	{
		prelast = last;
		last = last->next;
		
	}
	for(int i =0; i<num; i++)
	{
		if(i != 0/*last!= people*/){last = new person();}
		strcpy(last->name ,_people[i] );
		last->State = invited;
		if(/*last!= people*/i != 0)
		{
			prelast->next = last;
		}
			prelast = last;//prelast->next;
		//}
		if(i != (num-1))last = 0;
		
	}
	last->next = new person;
}

char* event::getstarttime()
{
	char  before[49] = "The event", after[] = "begins at %H:%M on %d-%m-%Y";
	strcat(before, " ");
	strcat(before, name);
	strcat(before, " ");
	strcat(before, after);
	char* target = new char[49];
	strftime(target,49, before , &start_time);
	return target;
}
char* event::getendtime()
{
	char  before[47] = "The event", after[] = "ends at %H:%M on %d-%m-%Y";
	strcat(before, " ");
	strcat(before, name);
	strcat(before, " ");
	strcat(before, after);
	char* target = new char[47];
	strftime(target,47, before , &end_time);
	return target;
}
char** event::getinvitedpeople()
{
	person* iterator = people;
	char** out;
	out = new char*[(const int)getpeoplenum()];
	int i = 0;
	while(i < getpeoplenum())
	{
		out[i] = new char[30];
		strcpy(out[i], iterator->name);
		i++;
		iterator = iterator->next;
	}
	return out;
	
}


void event::edit_start(int hour , int minute, int day, int month , int year )
{
	start_time.tm_hour = hour;
	start_time.tm_min = minute;
	start_time.tm_mday = day;
	start_time.tm_mon = --month;
	start_time.tm_year = year-1900;
}

void event::edit_end(int hour , int minute, int day, int month , int year )
{
	end_time.tm_hour = hour;
	end_time.tm_min = minute;
	end_time.tm_mday = day;
	end_time.tm_mon = --month;
	end_time.tm_year = year-1900;
}

void event::goingperson(char* one)
{
	person* p = event::findperson(one);
	p->State = going;
}

void event::notgoingperson( char *one )
{
	person* p = event::findperson(one);
	p->State = notgoing;
}

event::event(const event & N)
{
	strcpy(name, N.name);
	end_time = N.end_time;
	start_time = N.start_time;
	person* source = N.people, *last = people, *prelast = people;
	while(source != 0)
	{
		if(source->next == 0) {people = new person;break;}
		last = new person;
		strcpy(last->name, source->name);
		last->State = source->State;
		if(prelast != last)prelast->next = last;
		if(/*prelast->next != 0 &&*/ prelast != last)prelast = prelast->next;
		last = 0;
		source = source->next;
	}
	prelast = new person;
}
event::~event()
{
	person* iterator = people->next;
	while( people != 0)
	{
		delete people;
		if(iterator != 0)
		{
			people = iterator;
			iterator = iterator->next;
		}
		else break;
	}
}